package com.tim.itext;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.html.simpleparser.StyleSheet;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import com.tim.framework.util.FileUtil;
import com.tim.openoffice.PdfProc;

public class TestItext {
	public static void main(String[] args) {
		// writeImage("D:/img/jpg.jpg");
		getPdfFromHtml("D:/img/table-css.html", "D:/a/html.pdf");
	}

	@SuppressWarnings("unchecked")
	public static void getPdfFromHtml(String htmlPath, String pdfPath) {
		Document document = new Document();
		try {
			StyleSheet st = new StyleSheet();
			st.loadTagStyle("body", "leading", "16,0");
			PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
			document.open();
			BaseFont bfChinese = BaseFont.createFont("STSong-Light",
					"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
			new Paragraph(htmlPath, FontChinese);
			ArrayList list = HTMLWorker.parseToList(new FileReader(htmlPath),
					st);
			for (int k = 0; k < list.size(); ++k) {
				document.add((Element) list.get(k));
			}
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void writeImage(String imgPath) {
		try {
			// 写PDF文件.
			BufferedImage img = ImageIO.read(new File(imgPath));
			File pdfFile = FileUtil.createNewFile("D:/a/test.pdf");
			FileOutputStream fos = new FileOutputStream(pdfFile);
			// 创建PDF文档
			Document doc = new Document(null, 0, 0, 0, 0);
			// 设置尺寸为图片尺寸
			doc.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));

			Image image = Image.getInstance(imgPath);
			// 设定图片的宽度&高度(自动缩放，但图片所占空间不变)
			// image.scaleAbsolute(width, height);
			// 设定缩放比率50%
			// image.scalePercent(50);
			// 保持原有宽高比缩放
			// image.scaleToFit(300, 300);
			// 逆时针旋转指定度数(不自动缩放图片，可能会有部分角度遮住部分图片)
			// image.setRotationDegrees(90);
			// image.setRotation(45);
			PdfWriter.getInstance(doc, fos);
			doc.open();
			doc.add(image);
			doc.close();

			PdfProc.convertPDF2SWF("D:/a/test.pdf");
			System.out.println("完成转换了");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 写入字符串.
	 */
	public static void writeString(String content) {
		// 创建PDF文档
		Document doc = new Document();
		try {
			File pdfFile = new File("D:/a/test.pdf");
			FileOutputStream fos = new FileOutputStream(pdfFile);
			// 写PDF文件.
			PdfWriter.getInstance(doc, fos);
			doc.open();
			doc.add(new Paragraph(content));

			BaseFont bfChinese = BaseFont.createFont("STSong-Light",
					"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
			doc.add(new Paragraph("中文，你好.....", fontChinese));
			doc.close();

			PdfProc.convertPDF2SWF(pdfFile);
			System.out.println("处理完毕....");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
